package com.grechur.wanandroidcompose.http

import android.content.Context
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

object NetworkRequest {
    /**
     * 创建服务
     */
//    private val projectService = RetrofitClient.create(ProjectService::class.java)

//    public suspend fun getProjectTitles() = projectService.getProjectTitles().await()
//    public suspend fun getProjectTitles() = projectService.getProjectTitles()

//    public suspend fun getProjectArticles(index:Int?,cid:String?) = projectService.getProjectArticle(index,cid).await()
//    public suspend fun getProjectArticles(index:Int?,cid:String?) = projectService.getProjectArticle(index,cid)

    fun <T> getService(context: Context, clazz: Class<T>):T = RetrofitClient.create(context, clazz)

    /**
     * Retrofit网络返回处理
     */
//    private suspend fun <T> Call<T>.await(): T = suspendCoroutine {
//        enqueue(object : Callback<T> {
//            //正常返回
//            override fun onResponse(call: Call<T>, response: Response<T>) {
//                val body = response.body()
//                if (body != null) it.resume(body)
//                else it.resumeWithException(RuntimeException("response body is null"))
//            }
//
//            //异常返回
//            override fun onFailure(call: Call<T>, t: Throwable) {
//                it.resumeWithException(t)
//            }
//        })
//    }
}